package org.dromara.genealogy.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.genealogy.domain.vo.GeGenealogyVo;
import org.dromara.genealogy.domain.bo.GeGenealogyBo;
import org.dromara.genealogy.service.IGeGenealogyService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 族谱信息
 *
 * @author qianjm
 * @date 2025-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/genealogy")
public class GeGenealogyController extends BaseController {

    private final IGeGenealogyService geGenealogyService;

    /**
     * 查询族谱信息列表
     */
    @SaCheckPermission("genealogy:genealogy:list")
    @GetMapping("/list")
    public TableDataInfo<GeGenealogyVo> list(GeGenealogyBo bo, PageQuery pageQuery) {
        return geGenealogyService.queryPageList(bo, pageQuery);
    }



    /**
     * 获取族谱信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:genealogy:query")
    @GetMapping("/{id}")
    public R<GeGenealogyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(geGenealogyService.queryById(id));
    }

    /**
     * 新增族谱信息
     */
    @SaCheckPermission("genealogy:genealogy:add")
    @Log(title = "族谱信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<GeGenealogyBo> add(@Validated(AddGroup.class) @RequestBody GeGenealogyBo bo) {
        return R.ok(geGenealogyService.insertByBo(bo));
    }

    /**
     * 修改族谱信息
     */
    @SaCheckPermission("genealogy:genealogy:edit")
    @Log(title = "族谱信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GeGenealogyBo bo) {
        return toAjax(geGenealogyService.updateByBo(bo));
    }

    /**
     * 删除族谱信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("genealogy:genealogy:remove")
    @Log(title = "族谱信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geGenealogyService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 测试生成世系图word
     *
     */
    @GetMapping("/generateWord")
    public R<GeGenealogyVo> generateWord(@RequestParam("geId") Long geId) {
        geGenealogyService.generateWord(geId);
        return R.ok();
    }
}
